package com.lzp.controller;

//import com.lzp.service.BatchService;
import com.lzp.batch.MyExcelReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author : lzp
 * @date : 2021/9/27 9:45
 * @describe : TODO
 * @vision 1.0
 */
@RestController
@RequestMapping("/bill/")
public class BillController {
    @Resource
    Job job;
    @Resource
    MyExcelReader myExcelReader;
    @Resource
    JobLauncher jobLauncher;
     @GetMapping("startBatch")
    public String startBatch(){
         myExcelReader.setResource(new ClassPathResource("bill"+ File.separator+"微信支付账单(20160826-20161126).csv"));
         try {

             jobLauncher.run(job,new JobParametersBuilder().addDate("date",new Date()).toJobParameters());
         } catch (JobExecutionAlreadyRunningException e) {
             e.printStackTrace();
         } catch (JobRestartException e) {
             e.printStackTrace();
         } catch (JobInstanceAlreadyCompleteException e) {
             e.printStackTrace();
         } catch (JobParametersInvalidException e) {
             e.printStackTrace();
         }
         return "success";
    }
}
